package com.zj.blessings;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.activeandroid.ActiveAndroid;
import com.zj.blessings.R;
import com.zj.blessings.adapter.GuideAdapter;
import com.zj.blessings.db.DBItem;
import com.zj.blessings.util.FileUtils;

public class InitActivity extends ZJActivity implements OnClickListener,
		OnPageChangeListener {
	private int mVersionCode = -1;
	private int mLastVersionCode;
	private ViewPager mPager;
	private GuideAdapter mAdapter;
	private View mGuideLastPage;
	private ImageView dot1, dot2, dot3, dot4;
	private ImageView mImgStart;
	private SharedPreferences mPreferences;
	private boolean isFromSetting = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.initial);
		Intent intent = getIntent();
		if (intent != null) {
			isFromSetting = intent.getBooleanExtra(Consts.GUIDE_FLAG, false);
		}
		findViews();
		if (isFromSetting) {
			mAdapter = new GuideAdapter(this);
			mPager.setAdapter(mAdapter);
			mImgStart.setVisibility(View.GONE);
		} else {
			// 延时显示loading img
			// try {
			// Thread.sleep(800);
			// } catch (InterruptedException e1) {
			// // TODO Auto-generated catch block
			// e1.printStackTrace();
			// }
			PackageManager pm = getPackageManager();
			try {
				PackageInfo pi = pm.getPackageInfo(getPackageName(), 0);
				mVersionCode = pi.versionCode;
			} catch (NameNotFoundException e) {
				e.printStackTrace();
			}

			mPreferences = getSharedPreferences(Consts.SP_NAME,
					Context.MODE_PRIVATE);
			mLastVersionCode = mPreferences.getInt(Consts.SP_KEY_LAST_VERSION,
					-1);
			DBItem.shrinkTable();
			if (mVersionCode != mLastVersionCode) {
				FileUtils.initSharePath(mContext);
				mAdapter = new GuideAdapter(this);
				mPager.setAdapter(mAdapter);
			} else {
				startMain();
			}
		}
	}

	private void startMain() {
		Intent intent;
		if (JokeApp.getInstance().isInitDataMode()) {
			intent = new Intent(InitActivity.this, RemoteCategoryActivity.class);
		} else {
			intent = new Intent(InitActivity.this, CategoryActivity.class);
		}
		startActivity(intent);
		finish();
	}

	private void findViews() {
		mPager = (ViewPager) findViewById(R.id.guidePager);
		mPager.setOnPageChangeListener(this);
		mGuideLastPage = findViewById(R.id.guideLastPage);
		dot1 = (ImageView) findViewById(R.id.guideDot1);
		dot2 = (ImageView) findViewById(R.id.guideDot2);
		dot3 = (ImageView) findViewById(R.id.guideDot3);
		dot4 = (ImageView) findViewById(R.id.guideDot4);
		mImgStart = (ImageView) findViewById(R.id.guideImgStart);
		mImgStart.setOnClickListener(this);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	public void onPageSelected(int index) {
		if (mAdapter == null) {
			return;
		}
		switch (index) {
		case 0:
			dot1.setImageResource(R.drawable.ic_dot_press);
			dot2.setImageResource(R.drawable.ic_dot_normal);
			dot3.setImageResource(R.drawable.ic_dot_normal);
			dot4.setImageResource(R.drawable.ic_dot_normal);
			break;
		case 1:
			dot1.setImageResource(R.drawable.ic_dot_normal);
			dot2.setImageResource(R.drawable.ic_dot_press);
			dot3.setImageResource(R.drawable.ic_dot_normal);
			dot4.setImageResource(R.drawable.ic_dot_normal);
			break;
		case 2:
			dot1.setImageResource(R.drawable.ic_dot_normal);
			dot2.setImageResource(R.drawable.ic_dot_normal);
			dot3.setImageResource(R.drawable.ic_dot_press);
			dot4.setImageResource(R.drawable.ic_dot_normal);
			break;
		case 3:
			dot1.setImageResource(R.drawable.ic_dot_normal);
			dot2.setImageResource(R.drawable.ic_dot_normal);
			dot3.setImageResource(R.drawable.ic_dot_normal);
			dot4.setImageResource(R.drawable.ic_dot_press);
			break;
		default:
			break;
		}

		if (index == (mAdapter.getCount() - 1)) {
			Animation animation = AnimationUtils.loadAnimation(mContext,
					R.anim.zoom_in);
			mGuideLastPage.startAnimation(animation);
			mGuideLastPage.setVisibility(View.VISIBLE);
		} else if (mGuideLastPage.getVisibility() == View.VISIBLE) {
			Animation animation = AnimationUtils.loadAnimation(mContext,
					R.anim.zoom_out);
			mGuideLastPage.startAnimation(animation);
			mGuideLastPage.setVisibility(View.GONE);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.guideImgStart:
			Editor editor = mPreferences.edit();
			editor.putInt(Consts.SP_KEY_LAST_VERSION, mVersionCode);
			editor.commit();
			startMain();
			break;
		}
	}

	@Override
	public void onBackPressed() {
		finish();
		// overridePendingTransition(R.anim.push_top_in,
		// R.anim.push_bottom_out);
		overridePendingTransition(R.anim.activity_zoom_in,
				R.anim.activity_zoom_out);
	}
}
